package com.yuneec.fly.presenters;

import com.yuneec.droneservice.ServiceManager;
import com.yuneec.droneservice.command.ModifyChannelValues;
import com.yuneec.droneservice.model.ControllerLocation;
import com.yuneec.droneservice.model.ControllerVersion;
import com.yuneec.droneservice.model.HardwareEvent;
import com.yuneec.droneservice.model.JoystickValues;
import com.yuneec.droneservice.request.OnListener;
import com.yuneec.droneservice.tools.RCLog;

public class ConstantPresenter implements OnListener<HardwareEvent> {
    private ControllerVersion controllerVersion;
    private ControllerLocation controllerLocation;
    private JoystickValues joystickValues;
    private int[] channels;
    private int rightWheel;
    private boolean isInit;
    private int btnValues;
    private static ConstantPresenter presenter = new ConstantPresenter();

    public static ConstantPresenter getInstance() {
        return presenter;
    }

    public void init() {
        btnValues = 0;
        rightWheel = 0;
        isInit = true;
    }

    public void deInit() {
        isInit = false;
        controllerVersion = null;
        controllerLocation = null;
        joystickValues = null;
        channels = null;
    }

    private static final int BUTTON_1 = 1;
    private static final int BUTTON_2 = 2;
    private static final int BUTTON_3 = 3;
    private static final int BUTTON_4 = 4;
    private static final int BUTTON_5 = 5;
    private static final int WHEEL = 21;
    private static final int WHEEL_LEFT = 1;
    private static final int WHEEL_RIGHT = 255;

    // 1~10 for Button  1~10
    // 11~20 for Switch 1~10
    // 21~30 for Wheel  1~10
    // 41 for Wifi
    // 42 for Battery
    // 43 for HDMI
    @Override
    public void onListener(HardwareEvent hardwareEvent) {
        if (!isInit) {
            return;
        }
        int flag = 1;
        switch (hardwareEvent.getHwId()) {
            case BUTTON_5:
                flag <<= 1;
            case BUTTON_4:
                flag <<= 1;
            case BUTTON_3:
                flag <<= 1;
            case BUTTON_2:
                flag <<= 1;
            case BUTTON_1:
                if (hardwareEvent.getValue() > 0) {
                    btnValues |= flag;
                } else {
                    btnValues &= ~flag;
                }
                ServiceManager.getInstance().sendDroneCommand(new ModifyChannelValues(9, 1, btnValues), null);
                break;
            case WHEEL:
                switch (hardwareEvent.getValue()) {
                    case WHEEL_LEFT:
                        if (rightWheel < 4096) {
                            rightWheel++;
                        }
                        break;
                    case WHEEL_RIGHT:
                        if (rightWheel > 0) {
                            rightWheel--;
                        }
                        break;
                }
                break;
        }
    }

    public void setChannels(int[] channels) {
        this.channels = channels;
    }

    public int[] getChannels() {
        return channels;
    }

    public int getRightWHeel() {
        return rightWheel;
    }


    public void setControllerVersion(ControllerVersion controllerVersion) {
        this.controllerVersion = controllerVersion;
    }

    public ControllerVersion getControllerVersion() {
        return controllerVersion;
    }

    public void setControllerLocation(ControllerLocation controllerLocation) {
        this.controllerLocation = controllerLocation;
    }

    public ControllerLocation getControllerLocation() {
        return controllerLocation;
    }

    public void setJoystickValues(JoystickValues joystickValues) {
        this.joystickValues = joystickValues;
    }

    public JoystickValues getJoystickValues() {
        return joystickValues;
    }
}
